<?php

namespace app\api\controller;

use app\api\model\Friends;
use app\api\model\Relation;
use app\api\validate\FriendValidate;
use app\common\controller\Api;
use think\Controller;
use think\Request;

/**
 * 亲友团
 */
class Friend extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 亲友团列表
     */
    public function index($parent_id)
    {
        $friends = Friends::getFriendsByParent($parent_id);
        $friends ? $this->success('请求成功', ['friends' => $friends]) : $this->error('没有数据', [], 2);
    }

    /**
     * 亲属关系
     */
    public function relation()
    {
        $relations = Relation::all();
        $relations ? $this->success('请求成功', compact('relations')) : $this->error('没有数据');
    }

    /**
     * 添加亲友
     */
    public function create($parent_id)
    {
        $data = $this->request->post();
        $validate = new FriendValidate();
        if (!$validate->batch()->check($data)) {
            $this->error($validate->getError());
        }
        if (!isset($data['user_id']) || !$data['user_id']) {
            $this->error('用户user_id不能为空');
        }
        $friend = Friends::getByUserId($data['user_id']);
        if ($friend) {
            $this->error('您已经成为亲友了，无需再次操作');
        }
        $result = Friends::add($parent_id, $data);
        $result ? $this->success('请求成功') : $this->error('添加失败');
    }

    /**
     * 亲友审核
     */
    /*public function check($friend_id)
    {
        $type = $this->request->post('type');
        if (!$type) {
            $this->error('类型不能为空');
        }
        $friend = Friends::find($friend_id);
        if (!$friend) {
            $this->error('亲友不存在');
        }
        switch ($type) {
            //拒绝
            case 1:
                $friend->status = 0;
                break;
            //同意
            case 2:
                $friend->status = 2;
                break;
            default:
                $this->error('类型错误');
                break;
        }
        $result = $friend->save();
        $result ? $this->success('请求成功') : $this->error('审核失败，请联系管理员');
    }*/

    /**
     * 亲友信息
     */
    public function read($friend_id)
    {
        $friend = Friends::with("relations")->find($friend_id);
        $friend ? $this->success('请求成功', compact("friend")) : $this->error("没有数据", [], 2);
    }

    /**
     * 修改亲友
     */
    public function update($friend_id)
    {
        $friend = Friends::find($friend_id);
        if (!$friend) {
            $this->error("该亲友不存在");
        }
        $data = $this->request->post();
        $validate = new FriendValidate();
        if (!$validate->batch()->check($data)) {
            $this->error($validate->getError());
        }
        $result = $friend->edit($data);
        $result ? $this->success('请求成功') : $this->error('编辑失败');
    }

    /**
     * 删除
     */
    public function delete($friend_id)
    {
        $friend = Friends::find($friend_id);
        if (!$friend) {
            $this->error("该亲友不存在");
        }
        $result = $friend->delete();
        $result ? $this->success('删除成功') : $this->error('删除失败');
    }
}
